package simbot.example.util;

import org.springframework.util.ObjectUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * 图片生成工具
 *
 * @author 哈哈
 * @date 2022/9/1
 */
public class GenerateImageUtil {

    public static String FONT = "宋体";

    public static void generateSizeImageJiaoYiHang(List<Map<String,String>> list, String path, String desc) {
        // 字体大小
        int fontTitileSize = 15;
        // 横线的行数
        int totalrow =list.size() + 2;
        // 竖线的行数
        int totalcol =4;


        // 行高
        int rowHeight = 40;
        // 单元格宽度
        int colWidth = 200;
        // 图片宽度
        int imageWidth = colWidth* totalcol;
        // 图片高度
        int imageHeight = rowHeight* totalrow;
        // 起始宽度
        int startWidth = (imageWidth - (totalcol * colWidth)) / 2;
        // 起始高度
        int startHeight = 20;

        BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
        Graphics graphics = image.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, imageWidth, imageHeight);
        graphics.setColor(new Color(220, 240, 240));
        //设置字体
        Font font;
        //写入内容
        for (int n = 0; n < list.size(); n++) {
            for (int l = 0; l < 4; l++) {
                if (n == 0) {
                    font = new Font(FONT, Font.BOLD, fontTitileSize);
                    graphics.setFont(font);
                    graphics.setColor(Color.BLACK);
                } else if (n > 0 && l > 0) {
                    font = new Font(FONT, Font.BOLD, fontTitileSize);
                    graphics.setFont(font);
                    graphics.setColor(Color.gray);
                } else {
                    font = new Font(FONT, Font.BOLD, fontTitileSize);
                    graphics.setFont(font);
                    graphics.setColor(Color.BLACK);
                }
                //向单元格里写内容，水平居中&垂直居中
                FontMetrics fontMetrics = graphics.getFontMetrics(font);
                String content = list.get(n).get(String.valueOf(l));
                if (ObjectUtils.isEmpty(content)){
                    continue;
                }
                //获取写入内容的宽度
                int contentWidth = fontMetrics.stringWidth(content);
                //获取字体的高度
                int contentHeight = fontMetrics.getHeight();
                //获取字体的下降值
                int contentDescent = fontMetrics.getDescent();
                if(n==0){
                    graphics.drawString(content, imageWidth*(l+1)/ 3, startHeight);
                    continue;
                }
                //计算x、y坐标
                graphics.drawString(content,  colWidth * l + (colWidth - contentWidth) / 2, startHeight + rowHeight * (n + 1) - contentHeight / 2 - contentDescent);
            }
        }
        //设置备注
        font = new Font(FONT, Font.BOLD, fontTitileSize);
        graphics.setFont(font);
        graphics.drawString(desc, startWidth, imageHeight-20);
        // 保存图片
        createImage(image, path);
    }

    public static void generateSizeImage(String cellsValue[][], String path, String desc) {
        // 字体大小
        int fontTitileSize = 15;
        // 横线的行数
        int totalrow = cellsValue.length + 2;
        // 竖线的行数
        int totalcol = 0;
        if (cellsValue[0] != null) {
            totalcol = cellsValue[1].length;
        }
        // 图片宽度
        int imageWidth = 150* totalcol;
        // 图片高度
        int imageHeight = 40* totalrow;
        // 行高
        int rowHeight = 40;
        // 单元格宽度
        int colWidth = 140;
        // 起始宽度
        int startWidth = (imageWidth - (totalcol * colWidth)) / 2;
        // 起始高度
        int startHeight = 20;

        BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
        Graphics graphics = image.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, imageWidth, imageHeight);
        graphics.setColor(new Color(220, 240, 240));
        //设置字体
        Font font;
        //写入内容
        for (int n = 0; n < cellsValue.length; n++) {
            for (int l = 0; l < cellsValue[n].length; l++) {
                if (n == 0) {
                    font = new Font(FONT, Font.BOLD, fontTitileSize);
                    graphics.setFont(font);
                    graphics.setColor(Color.BLACK);
                } else if (n > 0 && l > 0) {
                    font = new Font(FONT, Font.BOLD, fontTitileSize);
                    graphics.setFont(font);
                    graphics.setColor(Color.gray);
                } else {
                    font = new Font(FONT, Font.BOLD, fontTitileSize);
                    graphics.setFont(font);
                    graphics.setColor(Color.BLACK);
                }
                //向单元格里写内容，水平居中&垂直居中
                FontMetrics fontMetrics = graphics.getFontMetrics(font);
                String content = cellsValue[n][l];
                if (ObjectUtils.isEmpty(content)){
                    continue;
                }
                //获取写入内容的宽度
                int contentWidth = fontMetrics.stringWidth(content);
                //获取字体的高度
                int contentHeight = fontMetrics.getHeight();
                //获取字体的下降值
                int contentDescent = fontMetrics.getDescent();
                if(n==0){
                    graphics.drawString(content, imageWidth*(l+1)/ 3, startHeight);
                    continue;
                }
                //计算x、y坐标
                graphics.drawString(content,  colWidth * l + (colWidth - contentWidth) / 2, startHeight + rowHeight * (n + 1) - contentHeight / 2 - contentDescent);
            }
        }
        //设置备注
        font = new Font(FONT, Font.BOLD, fontTitileSize);
        graphics.setFont(font);
        graphics.drawString(desc, startWidth, imageHeight-20);
        // 保存图片
        createImage(image, path);
    }

    /**
     * 将图片保存到指定位置
     *
     * @param image        缓冲文件类
     * @param fileLocation 文件位置
     */
    private static void createImage(BufferedImage image, String fileLocation) {
        try {
            ImageIO.write(image, "png", new File(fileLocation));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

